using System;
using System.Text;

namespace Aks.Deployment.SharePoint.Data
{
	/// <summary>
	/// Represents a CAML order by field node.
	/// </summary>
	internal class CamlOrderByField : CamlField
	{
		/// <summary>Field reference XML format. Arguments include: (0) field name.</summary>
		protected new const string CamlFormat = @"<FieldRef Name='{0}' Ascending='{1}'/>";


		/// <summary>
		/// Initializes a new instance of the <see cref="CamlOrderByField"/> class.
		/// </summary>
		/// <param name="name">The field name.</param>
		public CamlOrderByField(string name) : this(name, true)
		{}


		/// <summary>
		/// Initializes a new instance of the <see cref="CamlField"/> class.
		/// </summary>
		/// <param name="name">The field name.</param>
		/// <param name="ascending">Indicates whether to sort the field ascending.</param>
		public CamlOrderByField(string name, bool ascending) : base(name)
		{
			this.Ascending = ascending;
		}


		/// <summary>
		/// Gets or sets a value indicating whether this <see cref="CamlOrderByField"/> is ascending.
		/// </summary>
		/// <value><c>true</c> if ascending; otherwise, <c>false</c>.</value>
		public bool Ascending { get; set; }


		/// <summary>
		/// Returns a string that represents the current CAML node.
		/// </summary>
		/// <returns>
		/// A string representation of the CAML node.
		/// </returns>
		public override string ToCaml()
		{
			return String.Format(CamlFormat, Name, this.Ascending);
		}


		/// <summary>
		/// Returns a string that represents the specified CAML order by <paramref name="fields"/>.
		/// </summary>
		/// <param name="fields">The fields.</param>
		/// <returns>A string representation of the CAML order by fields.</returns>
		public new static string GetFieldList(params string[] fields)
		{
			var caml = new StringBuilder();
			for (var i = 0; i < fields.Length; i++)
			{
				caml.AppendFormat(CamlFormat, fields[i], true);
			}
			return caml.ToString();
		}
	}
}